package chis.dip.model;

import java.util.ArrayList;
import java.util.List;

import com.google.android.gms.maps.model.LatLng;

import android.content.res.AssetManager;

import chis.dip.util.ReadTextFile;

public class MapFeatureManager {

	private List<MapFeature> featureList;

	public MapFeatureManager(AssetManager assetManager) {
		featureList = new ArrayList<MapFeature>();
		initialiseFeatureData(assetManager);
	}

	private void initialiseFeatureData(AssetManager assetManager) {
		ReadTextFile r = new ReadTextFile();
		String s = r.readText("texts/mapfeatures.txt",assetManager);
		loadObjects(s);

	}

	private void loadObjects(String dataString) {
		if (dataString != null) {
			String[] feature = dataString.split(";");

			for (String f : feature) {
				String[] currentFeature = f.split(",");
				MapFeature mf = new MapFeature(currentFeature[0], 
						new LatLng(Double.parseDouble(currentFeature[1]),Double.parseDouble(currentFeature[2])), 
						currentFeature[4], 
						new MapFeatureType(currentFeature[5]));
				addFeature(mf);
			}

		}

	}

	public boolean addFeature(MapFeature feature) {
		if (featureList.contains(feature)) return false;
		featureList.add(feature);
		return true;
	}

	public boolean removeFeature(String featureName) {
		for(MapFeature f : featureList) {
			if (f.getName().equals(featureName)) {
				featureList.remove(f);
				return true;
			}
		}
		return false;
	}

	public List<MapFeature> getMapFeatures() {
		return featureList;
	}

}
